﻿
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using star_questionnair.Models;
using star_questionnair.Services;
using star_questionnair.Utilities;


namespace star_questionnair.Controllers;
[ApiController]
[Route("history")]
public class HistoryController : ControllerBase
{
    private readonly IHistoryService _historyService;

    public HistoryController(IHistoryService historyService)
    {
        _historyService = historyService; // 注入服务实例
    }

    [HttpGet("{id}/questionnaire")]
    public IActionResult GetHistoryListQuestionnaire(int id)
    {
        try
        {
            var questionnaires = _historyService.GetListQuestionnaireByUserId(id);
            if (questionnaires == null || !questionnaires.Any())
            {
                return NotFound(new { message = $"No questionnaires found for user ID {id}." });
            }

            return Ok(new { success = true, data = questionnaires });
        }
        catch (Exception ex)
        {
            return BadRequest(new { message = ex.Message });
        }
    }
    
    //得到个人的全部作答
    [HttpGet("responsePersonally/{userId}")]
    public async Task<IActionResult> GetResponseByUserId(int userId)
    {
        var responses = await _historyService.GetListResponseByUserId(userId);
        return Ok(new { success = true, data = responses });
    }
    
    //作者问卷
    [HttpGet("questionnaireBySelf/{userId}")]
    public IActionResult GetQuestionnaireBySelf(int userId)
    {
        try
        {
            var questionnaires = _historyService.GetQuestionnaireBySelf(userId);
        
            if (questionnaires == null || !questionnaires.Any())
            {
                return NotFound(new { message = $"No questionnaires found for author ID {userId}." });
            }

            return Ok(new{success=true, data=questionnaires});
        }
        catch (Exception ex)
        {
            return BadRequest(new { message = ex.Message });
        }
    }
    
    //查询问卷id对应的问题
    [HttpGet("{questionnaireId}/question")]
    public IActionResult GetListQuestionOfQuestionnaire(int questionnaireId)
    {
        try
        {
            var questions = _historyService.GetListQuestionByQuestionnaireId(questionnaireId);
        
            if (questions == null || !questions.Any())
            {
                return NotFound(new { message = $"No questions found for questionnaire ID {questionnaireId}." });
            }

            return Ok(new{success=true, data = questions});
        }
        catch (Exception ex)
        {
            return BadRequest(new { message = ex.Message });
        }
    }
    [HttpGet("wholeQuestionnaire/{questionnaireId}")]
    public IActionResult GetWholeQuestionnaire(int questionnaireId)
    {
        try
        {
            var questionnaire = _historyService.GetWholeQuestionnaire(questionnaireId);
            return Ok(new{success=true,data=questionnaire}); // 返回问卷数据
        }
        catch (Exception ex)
        {
            return BadRequest(new { message = ex.Message });
        }
    }
}